package com.wjj.controller;

import com.wjj.dto.ClassDTO;
import com.wjj.dto.StudentDTO;
import com.wjj.service.ClassService;
import com.wjj.service.StudentInternalService;
import java.util.List;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Description
 * @Author junjun Wang
 * @Date Create in 20:28 2023/12/7
 **/
@RestController
public class ClassController {

	@Autowired
	private ClassService classService;

	@Autowired
	private StudentInternalService studentInternalService;

	@Autowired
	private RestTemplate restTemplate;

	private static final String URL = "http://student-service";

	@GetMapping("/all")
	public List<ClassDTO> getClasses() {
		return classService.getClasses();
	}

	@GetMapping("/get/{classId}")
	public ClassDTO getClassById(@PathVariable("classId") @NotNull Integer classId) {
		return classService.getClassById(classId);
	}

	@PostMapping("/add")
	public void addClass(@RequestBody @NotEmpty List<ClassDTO> classes) {
		classService.addClass(classes);
	}

	@PutMapping("/update")
	public void updateClass(@RequestBody @NotNull ClassDTO classDTO) {
		classService.updateClass(classDTO);
	}

	@DeleteMapping("/delete/{classId}")
	public void deleteClassById(@PathVariable("classId") @NotNull Integer classId) {
		classService.deleteClassById(classId);
	}

	@GetMapping("/get-student/{classId}")
	public List<StudentDTO> getStudentsByClassId(
			@PathVariable("classId") @NotNull Integer classId) {
		// Ribbon方式访问
//		return restTemplate.getForObject(URL + "/student-get/" + classId, List.class);

		// feign 方式访问
		return studentInternalService.getStudentsByClassId(classId);
	}
}
